<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

/**
 * Generic frontend breadcrumb block
 *
 * Block parameters :
 * - displayMenuWoIndex (boolean) - Display menus without index page ?
 * - lastItemLink (boolean) - Display link on last item ?
 * - separator (string) - Breadcrumb items separator
 */
class m_website_actions_block_front_BreadCrumbAction extends f_core_Action
{
	public function execute()
	{
        $page = $this->getContext('currentPage');
        $website = $this->getContext('currentWebsite');

        // for news per example, get url suffix parameter
		$urlSuffix = $this->getRequestParameter("urlSuffix");

        // breadcrumb for this page is in cache ?
        if(is_null($urlSuffix) && $this->isTemplateCached("front/block/BreadCrumb.html", $page->getUniqueId(), $page->getLang()."|page|breadcrumb"))
        {
            return $this->getTemplateCacheContent();
        }

        // must we display menus without index page ?
        $displayMenuWoIndex = $this->getBlockParameter('displayMenuWoIndex', false);

        // must we add link on the last item ?
        $lastItemLink = $this->getBlockParameter('lastItemLink', false);
        
        // separator string
        $separator = $this->getBlockParameter('separator', '/');

        // get breadcrumb items
        $items = $this->getBreadCrumbItems($page, $displayMenuWoIndex);

        $putInCache = true;
        // if url has a suffix after page url
        if(!is_null($urlSuffix))
		{
            $doc = $this->getDocumentProvider() ->loadDocuments($urlSuffix['model'])
                                                ->whereIsEqual('url', $urlSuffix['url'])
                                                ->whereIsEqual('websiteId', $website->getUniqueId())
                                                ->retrieveFirstDocument($page->getLang());

            if(!is_null($doc) && method_exists($doc, "getMenuLabel") && method_exists($doc, "getUrl"))
            {
                $result["label"] = $doc->getMenuLabel();
                if(method_exists($doc, 'getMenuHoverLabel'))
                {
                    $result["menuHoverLabel"] = $doc->getMenuHoverLabel();
                }
                else
                {
                    $result["menuHoverLabel"] = $doc->getMenuLabel();
                }
                $result["href"] = $page->getCompleteUrl().'/'.$page->getUniqueId().'/'.$doc->getUniqueId().'/'.$doc->getUrl();
                $items[] = $result;
                $putInCache = false;
            }
		}

        // if last item must not have a link, remove it !
        if($lastItemLink == false)
        {
            $items[count($items)-1]['href'] = '';
        }

        $this->addToModel("page", $page);
        $this->addToModel("putInCache", $putInCache);
		$this->addToModel("separator", $separator);
		$this->addToModel("breadCrumbItems", $items);
        $this->setView('success');
	}

    /**
     * Get breadcrumb items in an array
     *
     * @param m_website_documents_page $page
     * @param boolean $displayMenuWoIndex
     * @return array
     */
    private function getBreadCrumbItems($page, $displayMenuWoIndex)
    {
        $relManager = f_relation_Manager::getInstance();

        $breadCrumbItems = array();

        $ancestors = $relManager->getParentsByDocType($page->getUniqueId(), array('modules_website/website', 'modules_website/menu'));

        foreach($ancestors as $k => $ancestor)
        {
            $item = array();
            if($ancestor->getModel() == 'modules_website/website')
            {
                /* @var $ancestor m_website_documents_Website */
                $home = $ancestor->getHomePage();
                $label = $ancestor->getLabel();
                if(is_object($home) && $home instanceof m_website_documents_Page)
                {
                    $label = $home->getMenuLabel();
                    $item['label'] = $label;
                    $item["menuHoverLabel"] = $home->getMenuHoverLabel();
                    $item['href'] = 'http://'.$ancestor->getRealUrl();
                    $breadCrumbItems[] = $item;
                }
            }
            else
            {
                /* @var $ancestor m_website_documents_Menu */

                if($ancestor->getMenuPageId() > 0 || ($ancestor->getMenuPageId() == 0 && $displayMenuWoIndex))
                {
                    if($ancestor->isVisibleInBreadCrumb())
                    {
                        $item['label'] = $ancestor->getLabel();
                        $item["menuHoverLabel"] = $ancestor->getMenuHoverLabel();
                        $item['href'] = $ancestor->getUrl();
                        $breadCrumbItems[] = $item;
                    }
                }
            }
        }

        if($breadCrumbItems[count($breadCrumbItems) - 1]['href'] != $page->getCompleteUrl())
        {
            /* @var $page m_website_documents_Page */
            $item['label'] = $page->getMenuLabel();
            $item["menuHoverLabel"] = $page->getMenuHoverLabel();
            $item['href'] = $page->getCompleteUrl();
            $breadCrumbItems[] = $item;
        }

        return $breadCrumbItems;
    }
}